package servlets;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import EJBPackage.Abilita;

import ejbSessions.ManagerAbilitaRemote;
import ejbSessions.ManagerUtenteRemote ;

/**
 * Servlet implementation class AbilitaUtente
 */
public class AbilitaUtente extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AbilitaUtente() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		gestisciRichiesta(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		gestisciRichiesta(request, response);
	}

	private void gestisciRichiesta(HttpServletRequest request, HttpServletResponse response){
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Utente/AbilitaUtente.jsp");
		int idUtente;
		
		try{
			Hashtable<String,String> env = new Hashtable<String,String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			Object ref = jndiContext.lookup("ManagerUtente/remote");
			Object ref2 = jndiContext.lookup("ManagerAbilita/remote");
			ManagerUtenteRemote manager = (ManagerUtenteRemote)ref;
			ManagerAbilitaRemote managerAbilita = (ManagerAbilitaRemote)ref2;
			idUtente = (Integer)request.getSession().getAttribute("userid");
			Set<Abilita> abilita = manager.getAbilita(idUtente);
			List<Abilita> tutteAbilita = managerAbilita.getTutteAbilita();

			if(request.getParameter("update")!=null){
				String[] ckbxAbilita =  request.getParameterValues("ckbxAbilita");
				
				//Controllo se sono state aggiunte
				for(String s: ckbxAbilita){
					Iterator<Abilita> i = abilita.iterator();
					boolean trovato = false;
					int idAbilitaCurr = Integer.parseInt(s);
					while(i.hasNext()){
						Abilita a = i.next();
						if(a.getId() == idAbilitaCurr){
							trovato=true;
						}
					}
					if(!trovato){
						managerAbilita.aggiuntaAbilitaUtente(idUtente, idAbilitaCurr);
					}
				}
				
				//Controllo se sono state tolte
				Iterator<Abilita> i = abilita.iterator();
				while(i.hasNext()){
					Abilita a = i.next();
					boolean trovato = false;
					for(String s: ckbxAbilita){
						if(Integer.parseInt(s) == a.getId()){
							trovato = true;
							break;
						}
					}
					if(!trovato){
						managerAbilita.deleteAbilitaUtente(idUtente, a.getNome());
					}
				}
					
			}
			abilita = manager.getAbilita(idUtente);
			request.getSession().setAttribute("tutteabilita", tutteAbilita);
			request.getSession().setAttribute("abilita", abilita);
		}catch(Exception e){
			try {
				response.sendRedirect("./");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
}
